<?php

$objXajax->registerFunction("fmfrmcongig_loadConfig");
$objXajax->registerFunction("fmfrmcongig_save");
//$objXajax->registerFunction("fmfrmcongig_edit");  
$objXajax->registerFunction("fmfrmcongig_loadshowmenu");  
$objXajax->registerFunction("fmfrmcongig_updateSubmenu");  

function fmfrmcongig_save($params){
    global $objDbUpdate,$hotel_id,$user_id;
    $objResponse   = new xajaxResponse();
    $fm_print      =$params['showprintbtn'] ;
    
    $fm_cancelroom =$params['chkcancelroom'];
    $fm_undobtn    =$params['chkundo'];
    $fm_undotime   =$params['txtundotime'];
    $fm_column   =$params['fmcolumn']; 
    $fm_percent   =$params['txtPromotion']==''?0:$params['txtPromotion']; 

    $flg_auto_send_sms=  $params['chkAutoSMS'];
    $time_auto_send_sms =$params['txtautotimefrom'];
    
    $fm_greeting_bill=addslashes(strip_tags($params['txtgreetingbill']));
    $fm_start_bill=addslashes(strip_tags($params['txtstartbill']));
    $fm_name_service=addslashes(strip_tags($params['txtnameservice']));
    $phone_auto_send_sms=addslashes($params['txtphonesms']);

    $sql= "update cus_config SET  fm_greeting_bill = '$fm_greeting_bill',fm_print='$fm_print',
    fm_cancelbtn = '$fm_cancelroom',fm_undobtn=$fm_undobtn,fm_start_bill='$fm_start_bill',fm_name_service='$fm_name_service',
    fm_undotime='$fm_undotime',fm_column='$fm_column',fm_percent_promotion='$fm_percent',
    flg_auto_send_sms='$flg_auto_send_sms',time_auto_send_sms='$time_auto_send_sms',phone_auto_send_sms='$phone_auto_send_sms' 
    WHERE service_id = '$hotel_id'";
    if($_SESSION['current']['power_type_id']==POWER_CUSTOMER){
        $fm_cancelroom=1;
        $fm_undotime=1;
    }

    $objDbUpdate->Execute($sql);
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript("fm_reload($fm_print,'$fm_greeting_bill',$fm_cancelroom,$fm_undobtn,
    $fm_undotime,$fm_percent,'$fm_start_bill','$fm_name_service');"); 
    $objResponse->addAlert('Hoàn thành cập nhật cấu hình');
    return   $objResponse->getXML();
}
function fmfrmcongig_updateSubmenu($strid){
    global $objDbUpdate,$hotel_id,$user_id;
    $objResponse = new xajaxResponse();  
    $sql = "Delete from tbl_menu_for_subuser where service_id = $hotel_id";
    $objDbUpdate->Execute($sql);
    $arrShowId = explode(',',$strid);
    $count = count($arrShowId);
    if(count($arrShowId)>0){
        $sqlInsert ='INSERT INTO tbl_menu_for_subuser(user_create,date_create,service_id,menu_cus_id) values';
        $temp='';
        for($i=0;$i<$count;$i++){
           $menu_cus_id = $arrShowId[$i]; 
           $temp.=",($user_id,now(),$hotel_id,'$menu_cus_id')"; 
        }
        $temp =  substr($temp,1);
        $sqlInsert .=$temp;
        $objDbUpdate->Execute($sqlInsert);
        
    }
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addAlert('Hoàn thành cập nhật phân quyền tài khoản con!');      
    return   $objResponse->getXML();
}

function fmfrmcongig_loadConfig(){     
    global $lang,$objDbSelect,$hotel_id;
    $objResponse = new xajaxResponse();
    $sql= "select fm_print,fm_greeting_bill,fm_name_service,fm_start_bill,fm_mintime,fm_cancelbtn,fm_undobtn,fm_undotime,
    fm_column,fm_percent_promotion,flg_auto_send_sms,time_auto_send_sms,phone_auto_send_sms
     from  cus_config WHERE service_id = '$hotel_id'";
    $arr= $objDbSelect->GetArray($sql);

    $showprintbtn=$arr[0]['fm_print'] ;
    $fm_cancelbtn=$arr[0]['fm_cancelbtn'] ;
    

    
    $fm_greeting_bill =htmlspecialchars_decode($arr[0]['fm_greeting_bill']) ;
    $fm_start_bill =htmlspecialchars_decode($arr[0]['fm_start_bill']) ;
    $fm_name_service =htmlspecialchars_decode($arr[0]['fm_name_service']) ;
   
      
    $fm_undobtn=$arr[0]['fm_undobtn'] ;
    $txtundotime=$arr[0]['fm_undotime'] ;
    $fm_column=$arr[0]['fm_column'] ;
    $fm_percent=$arr[0]['fm_percent_promotion'] ;
    $flg_auto_send_sms=$arr[0]['flg_auto_send_sms'] ;
    $time_auto_send_sms=$arr[0]['time_auto_send_sms'] ;
    $phone_auto_send_sms=$arr[0]['phone_auto_send_sms'] ;

     if($showprintbtn==1){
        $objResponse->addScript("$('#fmfrmcongig_showprintbtn').attr('checked','checked')"); 
      
     }else{
          $objResponse->addScript("$('#fmfrmcongig_showprintbtn').attr('checked',false)");
    }     
     if($fm_cancelbtn==1){
        $objResponse->addScript("$('#fmfrmcongig_chkcancelroom').attr('checked','checked')"); 
      
     }else{
          $objResponse->addScript("$('#fmfrmcongig_chkcancelroom').attr('checked',false)");
    }    
    if($fm_undobtn==1){
        $objResponse->addScript("$('#fmfrmcongig_chkundo').attr('checked','checked')"); 
      
     }else{
          $objResponse->addScript("$('#fmfrmcongig_chkundo').attr('checked',false)");
    }    
    if($flg_auto_send_sms==1){
        $objResponse->addScript("$('#fmfrmcongig_chkAutoSMS').attr('checked','checked')"); 
      
     }else{
          $objResponse->addScript("$('#fmfrmcongig_chkAutoSMS').attr('checked',false)");
    }
    
     $objResponse->addAssign('fmfrmcongig_txtgreetingbill','value',$fm_greeting_bill);     
     $objResponse->addAssign('fmfrmcongig_txtstartbill','value',$fm_start_bill);     
     $objResponse->addAssign('fmfrmcongig_txtnameservice','value',$fm_name_service);
          
    $objResponse->addScript("$('#fmfrmcongig_fmcolumn_$fm_column').attr('checked','checked')");                       
    $objResponse->addScript("$('#fmfrmcongig_txtundotime').val('$txtundotime')");                       
    $objResponse->addScript("$('#fmfrmcongig_txtPromotion').val('$fm_percent')");                       
    $objResponse->addScript("$('#fmfrmcongig_txtautotimefrom').val('$time_auto_send_sms')");                       
    $objResponse->addScript("$('#fmfrmcongig_txtautotimeto').val('$time_auto_send_sms')");                       
    $objResponse->addScript("$('#fmfrmcongig_txtphonesms').val('$phone_auto_send_sms')");                       
    $objResponse->addScript('changeWaiting(1);');                                                      
    return $objResponse->getXML();
}



function  fmfrmcongig_Getconfigure(){
global $objDbSelect;
$sqlSelect = 'select * from tbl_service_configure ';
return $objDbSelect->GetArray($sqlSelect);
}

function fmfrmcongig_loadshowmenu(){     

    global $lang,$hotel_id,$showbutton;
    $power_type_id=$_SESSION['current']['power_type_id'];
    $showbutton ='';
    if($power_type_id==POWER_SUB_CUSTOMER){
        $showbutton='none';
    }
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = fmfrmcongig_getshowmenu();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_config_showmenu_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmconfig_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('fmfrmconfig_tbl');");
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML(); 
}

function fmfrmcongig_getshowmenu(){
    global $lang,$hotel_id,$lang,$objDbSelect;
  
    $sql ="select m.id,m.name_vi as subname,c.name_vi as mainname,c.id as main_id,if(p.id is null,'','checked=\"checked\"')  as id_show ,
    if(p.id is null,'','row_selected')  as row_selected from tbl_main_menu_cus c join 
    tbl_menu_cus m on c.id=m.main_menu_id left join tbl_menu_for_subuser as p on p.menu_cus_id = m.id and service_id ='$hotel_id'
     order by c.ordershow,m.name_vi"; 
    $arr=$objDbSelect->GetArray($sql);
    return $arr;
}

  
  # end region process data
?>
